<?php
require_once('configure.php');
list($nik,$nama) = explode("/" ,$_GET['nama'] );
$nik = rtrim($nik,")");
$nik = trim($nik);
$tanggal = trim($_GET['date']);
$check = isset($_GET['check']) ? true : false;
if(trim($nik) == ''){echo"|";exit;}
$karyawan_id=check_karyawan_by_nik($nik);
$karyawan =  loaddata_karyawan($karyawan_id,false);  
list($yyyy,$mm,$dd) = explode("-" , $tanggal);
$year= (int) $yyyy;
$month= (int) $mm;
$day= (int) $dd;
if( $day > 10){
	if($month==12){
		$month=1;
		$year++;
	}else{
		$month++;
	}
}	

$temp=$year.'-'.sprintf( '%02d',$month).'-01' ;
$periode_absen=(int) get_periode_id_by_tanggal($temp) ;
$periode_id =get_periode_absen();
if( $periode_absen== $periode_id){ //kalau tanggal dalam range periode absen
	$ch = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id} 
		AND field_tgl='tgl_{$dd}' 
		ORDER BY realisasi_id 
		DESC LIMIT 1";
}else{
	$ch = "SELECT * FROM a_realisasi_log
		WHERE karyawan_id = {$karyawan_id} 
		AND field_tgl='tgl_{$dd}' 
		AND periode_id={$periode_absen} 
		ORDER BY realisasi_id 
		DESC LIMIT 1";


}		
$chr = my_query($ch);
if( my_num_rows($chr)==0){
	echo 'Belum ada| Belum ada | |';
	return ;
}	

$row = my_fetch_array($chr);
$status_absen_id=$row['sc_status_absen_id'];
$statuss=my_get_data_by_id('a_status_absen','status_absen_id',$status_absen_id);
$sc_jadwal_kerja_kelompok_id=(int) (isset($row['sc_jadwal_kerja_kelompok_id']) ? $row['sc_jadwal_kerja_kelompok_id'] : 0 );
$jadwal_kerja_kelompoks = my_get_data_by_id('a_jadwal_kerja_kelompok','jadwal_kerja_kelompok_id',$sc_jadwal_kerja_kelompok_id);
$waktu_kerja_id=(isset($jadwal_kerja_kelompoks['waktu_kerja_id']) ? $jadwal_kerja_kelompoks['waktu_kerja_id'] : 0 ) ;
$kelompok=(isset($jadwal_kerja_kelompoks['kelompok']) ? $jadwal_kerja_kelompoks['kelompok'] : '') ;
$hari_id=get_hari_id_by_date($tanggal);
$query = "SELECT jam_masuk,jam_pulang FROM a_jadwal_kerja 
		WHERE waktu_kerja_id =".$waktu_kerja_id."  
		AND kelompok='".$kelompok."' 
		AND hari_id={$hari_id}";
$jams = my_fetch_array(my_query($query));
$waktu_kerjas = my_get_data_by_id('a_waktu_kerja','waktu_kerja_id',$waktu_kerja_id)	;

if( isset($statuss['initial_absen']) ){
	$t = $statuss['initial_absen']." "  ;
}elseif( isset($waktu_kerjas['label']) ){
	$t = $waktu_kerjas['label']."/".$kelompok." " ;
}	
$t  .= " (".date('H:i',strtotime($jams['jam_masuk'])).' - '.date('H:i',strtotime($jams['jam_pulang'])).")"; 	

$status_absen_id=(int) $row['status_absen_id'];
$statuss=my_get_data_by_id('a_status_absen','status_absen_id',$status_absen_id);
$r  = $statuss['initial_absen']." (".gmdate('H:i',$row['jam_datang']).' - '.gmdate('H:i',$row['jam_pulang']).")"; 	
$statuss= get_status_absen($karyawan_id , $tanggal ) ;
$s='' ;
foreach($statuss as $status){
	$s.=$status.', ' ;
}
$s=substr($s,0,strlen($s)-2) ;
echo $t.'|'.$r .'|'.$s .'|';
if($check){ 
	if($karyawan['golongan_jabatan_id'] > 5){
		echo "0";
	}else{
		echo "1";
	}
}
 
function get_kelompok_jadwal($tanggal , $waktu_kerja_id , $kelompok ){
	$hari_id=date("N",strtotime($tanggal) );
	$datas = array();
	$query = "SELECT jadwal_kerja_id FROM a_jadwal_kerja 
			WHERE waktu_kerja_id = {$waktu_kerja_id} 
			AND kelompok='".$kelompok."' 
			AND hari_id={$hari_id}";
	$result = my_query($query);
	$row = my_fetch_array($result);	
	$res=isset( $row['jadwal_kerja_id'] ) ? $row['jadwal_kerja_id']:  0;
	return $res;
}

function get_hari_id_by_date($date){
	$hari_id=date("N",strtotime($date) ); //Monday to Sunday
	//konversi ke Sunday to Saturday
	if($hari_id==7){
		$hari_id=1;
	}else{
		$hari_id++;
	}		
	return $hari_id;
}
